package app.fugema.shop.main.order.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.GeneralRecyclerAdapter;
import com.base.common.view.adapter.bean.FooterBean;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;
import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.AddressManageAcBinding;
import app.fugema.shop.databinding.AddressManageFooterBinding;
import app.fugema.shop.databinding.AddressManageItBinding;
import app.fugema.shop.main.order.model.AddressResult;
import app.fugema.shop.main.home.vm.AddressVM;

public class AddressManageActivity extends BaseTitleActivity<AddressManageAcBinding, AddressVM> {

    private String userKey;
    /**
     * @param context
     * @param isSelectAddress 是否选择地址？
     */
    public static void start(Context context, boolean isSelectAddress) {
        Intent starter = new Intent(context, AddressManageActivity.class);
        starter.putExtra("isSelectAddress", isSelectAddress);
        context.startActivity(starter);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_manage_ac);
    }

    private GeneralRecyclerAdapter adapter;
    private boolean isSelectAddress;

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }
    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("收货地址",R.color.white);
        isSelectAddress = getIntent().getBooleanExtra("isSelectAddress", false);
        userKey = LoginUtils.getToken();
        adapter = new GeneralRecyclerAdapter() {
            @Override
            public void initMultiItemType()
            {
                putMultiItemType(new BaseItemMultiType<AddressResult.AddressBean, AddressManageItBinding>()
                {
                    @Override
                    public int getLayoutId()
                    {
                        return R.layout.address_manage_it;
                    }

                    @Override
                    public void onBindViewHolder(AddressManageItBinding binding, int position, AddressResult.AddressBean bean) {
                        super.onBindViewHolder(binding, position, bean);

                        binding.ivCheckBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                            {
                                 if( isChecked )
                                 {
                                     viewModel.setDefAddr(bean.getId() , userKey)
                                             .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>(getActivity())
                                             {
                                                 @Override
                                                 protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                                                 {
                                                     UIUtils.showToastSafes("默认地址设置成功");
                                                     initData();
                                                 }
                                             });

                                 }
                            }
                        });
                    }

                    @Override
                    public void onItemClick(View view, int onclickType, int position,AddressResult.AddressBean bean)
                    {
                        super.onItemClick(view, onclickType, position, bean);
                        switch (view.getId())
                        {
                            case R.id.rclItemView:
                                if (isSelectAddress)
                                {
                                    LiveEventBus.get(AppConstants.EventKey.ADDRESS_SELECT_COMPLETE, AddressResult.AddressBean.class).post(bean);
                                    finish();
                                }
                                break;
                            case R.id.iv_delete_btn:
                                //删除
                                viewModel.deleteAddress(bean.getId() ,userKey)
                                         .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>(getActivity())
                                         {
                                    @Override
                                    protected void onSuccess(LiveDataWrapper<BaseApiResult> data) {
                                        UIUtils.showToastSafes("地址删除成功");
                                        adapter.remove(position);
                                    }
                                });

                                break;
                            case R.id.iv_edit_btn:
                                //编辑
                                AddressAddActivity.start(getActivity(), bean.getId());
                                break;
                        }

                    }
                });

                putMultiItemType(new BaseItemMultiType<FooterBean, AddressManageFooterBinding>() {
                    @Override
                    public int getItemViewType() {
                        return TYPE_FOOT;
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.address_manage_footer;
                    }

                    @Override
                    public void onCreateViewHolder(AddressManageFooterBinding binding, BaseViewHolder viewHolder) {
                        super.onCreateViewHolder(binding, viewHolder);
                        binding.clItemView.setOnClickListener(viewHolder);
                    }

                    @Override
                    public void onItemClick(View view, int onclickType, int position, FooterBean bean) {
                        super.onItemClick(view, onclickType, position, bean);
                        AddressAddActivity.start(getActivity());
                    }
                });

            }
        };
        adapter.setSideslip(true);
        binding.rvRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onPauseRevert() {
        super.onPauseRevert();
        initData();
    }

    @Override
    protected void initData()
    {
        adapter.addFooterView(new FooterBean(BaseItemTypeInterface.TYPE_FOOT));

        viewModel.addressList(userKey).observe(this, new BaseViewObserver<LiveDataWrapper<AddressResult>>(getActivity())
        {
            @Override
            protected void onSuccess(LiveDataWrapper<AddressResult> data)
            {
                adapter.setDataList(data.data.getShippingAddress().getCanDeliveAddressList());
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<AddressResult> data)
            {
                return UIUtils.isEmpty(data.data.getShippingAddress().getCanDeliveAddressList());
            }
        });
    }
}
